<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>多轮对话测试</title>
    <style>
        body {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            font-family: Arial, sans-serif;
        }
        #messages {
            height: 500px;
            border: 1px solid #ddd;
            padding: 20px;
            margin-bottom: 20px;
            overflow-y: auto;
        }
        .message {
            margin: 10px 0;
            padding: 10px;
            border-radius: 5px;
        }
        .user-message {
            background-color: #e3f2fd;
            margin-left: 20%;
        }
        .bot-message {
            background-color: #ef0606;
            margin-right: 20%;
        }
        #input-area {
            display: flex;
            gap: 10px;
        }
        #user-input {
            flex: 1;
            padding: 10px;
            border: 1px solid #ddd;
        }
        button {
            padding: 10px 20px;
            background-color: #007bff;
            color: white;
            border: none;
            cursor: pointer;
        }
    </style>
</head>
<body>
<h1>多轮对话测试</h1>
<div id="messages"></div>
<div id="input-area">
    <input type="text" id="user-input" placeholder="输入消息...">
    <button onclick="sendMessage()">发送</button>
</div>

<script>
    let conversationHistory = [];

    async function sendMessage() {
        const userInput = document.getElementById('user-input');
        const message = userInput.value.trim();
        if (!message) return;

        // 添加用户消息到历史
        conversationHistory.push({ role: 'user', content: message });
        addMessageToUI(message, 'user');
        userInput.value = '';

        try {
            const response = await fetch('http://localhost:8080/topic/chat', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    messages: conversationHistory
                })
            });

            const data = await response.json();
            if (data.content) {
                conversationHistory.push({ role: 'assistant', content: data.content });
                addMessageToUI(data.content, 'assistant');
            }
        } catch (error) {
            console.error('请求失败:', error);
            addMessageToUI('请求失败，请检查控制台', 'bot');
        }
    }

    function addMessageToUI(text, role) {
        const messagesDiv = document.getElementById('messages');
        const messageDiv = document.createElement('div');
        messageDiv.className = `message ${role}-message`;
        messageDiv.textContent = text;
        messagesDiv.appendChild(messageDiv);
        messagesDiv.scrollTop = messagesDiv.scrollHeight;
    }

    // 回车键支持
    document.getElementById('user-input').addEventListener('keypress', (e) => {
        if (e.key === 'Enter') {
            //alert("123");
            sendMessage();
        }
    });
</script>
</body>
</html>
